/* This file is part of QGoogleTranslate
 *
 * Copyright (C) 2009 Durachenko Alexei V. <lexnewton86@gmail.com>
 */

#include "config.h"
#include "cconfigdialog.h"
#include <QtNetwork>

CConfigDialog::CConfigDialog( QWidget * parent, Qt::WFlags f)
	: QDialog(parent, f)
{
	setupUi(this);

    m_Settings = 0;

	for( int i = 0; i < gGoogleLangCount; ++i )
	{
        comboBox_LangSrc->addItem( gGoogleLang[i].name );
	    comboBox_LangDest->addItem( gGoogleLang[i].name );

        QString path = QString(":/flags/flags/none.png");
        QString flag = QString(":/flags/flags/%1.png").arg( gGoogleLang[i].code );
        if( QFile::exists( flag ) )
        {
            path = flag;
        }
        comboBox_LangSrc->setItemIcon( i, QIcon( path ) );
        comboBox_LangDest->setItemIcon( i, QIcon( path ) );
    }
    
    comboBox_ProxyType->addItem( tr("SOCK 5 Proxy"), QNetworkProxy::Socks5Proxy );
    comboBox_ProxyType->addItem( tr("HTTP PROXY"), QNetworkProxy::HttpProxy );
}

void CConfigDialog::setSettingsObject( QSettings* settings )
{
    m_Settings = settings;

    Q_ASSERT( m_Settings != 0 );
    if( m_Settings )
    {
        comboBox_LangSrc->setCurrentIndex( m_Settings->value("DefaultSourceLanguageIndex",0).toInt() );
        comboBox_LangDest->setCurrentIndex( m_Settings->value("DefaultDestinationLanguageIndex",0).toInt() );
        checkBox_TranslateOfBufferInNewTab->setChecked( m_Settings->value("TranslateOfBufferInNewTab", false ).toBool() );

        checkBox_UseProxyServer->setChecked( m_Settings->value( "UseProxyServer", false ).toBool() );
        lineEdit_ProxyHost->setText( m_Settings->value( "ProxyHost", "").toString() );
        lineEdit_ProxyUser->setText( m_Settings->value( "ProxyUser", "").toString() );
        lineEdit_ProxyPassword->setText( m_Settings->value( "ProxyPassword", "").toString() );
        spinBox_ProxyPort->setValue( m_Settings->value( "ProxyPort", 80 ).toInt() );

        int type = m_Settings->value( "ProxyType" ,-1 ).toInt();
        if( type != -1 )
        {
            int index = comboBox_ProxyType->findData( type );
            comboBox_ProxyType->setCurrentIndex( index );
        }
               
    }
}

void CConfigDialog::on_buttonBox_accepted()
{
    Q_ASSERT( m_Settings != 0 );
    if( m_Settings )
    {
        m_Settings->setValue( "DefaultSourceLanguageIndex", comboBox_LangSrc->currentIndex() );
        m_Settings->setValue( "DefaultDestinationLanguageIndex", comboBox_LangDest->currentIndex() );
        m_Settings->setValue( "TranslateOfBufferInNewTab", checkBox_TranslateOfBufferInNewTab->isChecked() );
    
    
        m_Settings->setValue( "UseProxyServer", checkBox_UseProxyServer->isChecked() );
        m_Settings->setValue( "ProxyHost", lineEdit_ProxyHost->text() );
        m_Settings->setValue( "ProxyPort", spinBox_ProxyPort->value() );
        m_Settings->setValue( "ProxyType", comboBox_ProxyType->itemData( comboBox_ProxyType->currentIndex() ).toInt() );
        m_Settings->setValue( "ProxyUser", lineEdit_ProxyUser->text() );
        m_Settings->setValue( "ProxyPassword", lineEdit_ProxyPassword->text() );
    }

	accept();
}
